<?php 
class galleriesAction extends AdminMaster{
	public function listFilter(){
		$this->data['filter']['albums']	= GalleryAlbums::getDataListFrontEnd();
		
	}
	public function showList(){
		$this->data['list']			 							= Galleries::getDataList($this->data['filter'], $this->data['pagination']);
		$this->data['pagination']['total']		= Galleries::getDataListCount($this->data['filter']);
		$this->data['pagination']['router']		= 'gallery_gallery';
		$this->data['pagination']['params']		= $this->data['filter'];
		
		
	}
	public function delRecord(){
		GalleriesTable::delete($this->data['select_list']);
	}
	public function disableRecord(){
		GalleriesTable::disable($this->data['select_list']);
	}
	public function enableRecord(){
		GalleriesTable::enable($this->data['select_list']);
	}
	public function saveOrder(){
		$sortData = array();
		for($i = 0; $i< count($this->data['ids']); $i++){
			$sortData[$this->data['ids'][$i]] = $this->data['sort_orders'][$i];
		}
		Galleries::saveSortOrder($sortData);
	}
	public function addnew(){
		$this->setTemplate('galleriesForm');

		$form = new GalleriesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			
			if($form->isValid()){
				$product_id = Galleries::insertUpdate($this->formData);
				if($product_id > 0){
					$this->data['result']	= __('add_new_success');
					$form = new GalleriesFForm();
				}else{
					$this->data['result']	= sprintf(__('add_new_error'), sfConfig::get('app_email_admin')) ;
				}
			}
		}
		$this->setVar('form', $form);
	}
	public function edit(){
		$this->setTemplate('galleriesForm');

		$form = new GalleriesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if(Galleries::insertUpdate($this->formData)){
					$this->data['result']	= __('edit_success');
				}else{
					$this->data['result']	= sprintf(__('edit_error'), sfConfig::get('app_email_admin')) ;
				}
			}
			if(!empty($this->formData['photo_url'])){
				$this->data['photo_url'] = $this->formData['photo_url'];
				$this->data['photo_remove']	= 1;
			}
		}else{
			$data = Galleries::getDataByIdAdmin($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				if(!empty($data['photo_url'])){
					$this->data['photo_url'] = $data['photo_url'];
					$this->data['photo_remove'] = 1;
				}
				
				$this->formData = array(
						'gallery_album_id'	=> $data['gallery_album_id'],
						'photo_url'					=> $data['photo_url'],
						'is_enabled'				=> $data['is_enabled'],
						'_csrf_token'=> $form->getCSRFToken()
				);
				foreach($this->data['langs'] as $lang){
					$this->formData['name_'.$lang] = $data['GalleriesI18n'][$lang]['name'];
					$this->formData['description_'.$lang] = $data['GalleriesI18n'][$lang]['description'];
					
				}
				$form->bind($this->formData);

			}
		}
		$this->setVar('form', $form);
	}
}